﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;
using System.Data;

public partial class MembershipCard_wechat_web_Products2 : WeiPage
{
    PRODUCTBO pbo = new PRODUCTBO();
    PersonInfoBO fbo = new PersonInfoBO();
    ShoppingCartBO sbo = new ShoppingCartBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["code"] != null)
            {
                try
                {
                    string url = string.Format(@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GetAppid() + "&secret="+GetSecret()+"&code={0}&grant_type=authorization_code", Request.QueryString["code"].ToString());
                    string token = PostRequest(url);
                    if (token.Contains("7200"))
                    {
                        string[] b = token.Split('\"');
                        Session["FromUserName"] = b[13];
                        Session["ToUserName"] = GetAppid();
                    }
                }
                catch (Exception)
                {

                }
            }

            if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
            {
                string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                Session["FromUserName"] = user;
                Session["ToUserName"] = user2;
                PersonInfo per = fbo.GetModel(user);
                if (per != null)
                {
                    List<ShoppingCart> list2 = sbo.GetModelList(" pid=" + per.ID);
                    if (list2.Count > 0)
                    {
                        sumcar.InnerHtml = "<span><a href='ShopCar2.aspx'><em class='indexicon4'></em><i>" + list2.Count + "</i></a></span>";
                    }
                }
            }

            xl.InnerHtml = "";
            DataTable dtXL = pbo.GetXL();
            foreach (DataRow dr in dtXL.Rows)
            {
                xl.InnerHtml += "<a href='Products2.aspx?h="+dr[0].ToString()+"' onclick='javascript:hidedd();'>"+dr[0].ToString()+"</a>";
            }

            string swhere = " 1=1 and IF_DISP=1 ";
            if (Request.QueryString["mx"] != null && Request["mx"].ToString() == "1")
            {
                swhere += " and IF_STAR=1 ";
                titie.Text = "明星产品";
            }
            else if (Request.QueryString["mx"] != null)
            {
                swhere += "  and  T_LEVEL1='" + Request.QueryString["mx"].ToString() + "' ";
                titie.Text = Request.QueryString["mx"].ToString();
                if (titie.Text == "美妆香氛")
                    titie.Text = "美妆护肤";
                if (titie.Text == "美妆护肤")
                {
                    image1.Src = "images/4.jpg";
                }
                else if (titie.Text == "珠宝")
                {
                    image1.Src = "images/2.jpg";
                }
                else if (titie.Text == "服饰配件")
                {
                    image1.Src = "images/1.jpg";
                }
            }
            if (Request.QueryString["top"] != null)
            {
                swhere += " and IF_TOP=1 ";
                titie.Text = "当季推荐";
                image1.Src = "images/3.jpg";
            }
            if (Request.QueryString["price"] != null)
            {
                if (Request.QueryString["price"].ToString() == "1")
                {
                    swhere += " and Price<=800 ";
                }
                else if (Request.QueryString["price"].ToString() == "2")
                {
                    swhere += " and price>=801 and price<=1200 ";
                }
                else if (Request.QueryString["price"].ToString() == "3")
                {
                    swhere += " and price>1200 ";
                }
               
                titie.Text = "商城";
            }

            if (Request.QueryString["h"] != null)
                swhere += "  and  T_LEVEL2='" + Request.QueryString["h"].ToString() + "' ";

            List<PRODUCT> list = pbo.GetModelList(swhere);
            plist.InnerHtml = "";
            foreach (PRODUCT p in list)
            {
                plist.InnerHtml += string.Format(@"<ol>
   <p>
   <a href='ProductsExchange2.aspx?id={3}'   ><img src='{0}'  style='max-height:287px;' alt='' /></a>
   <span>&nbsp;&nbsp;{1}</span>
   <span>&nbsp;&nbsp;价格：<strong>{2}元</strong></span>
   </p>
   </ol>
            ", (p.PicUrl.Trim() == "" ? "images/m.png" : p.PicUrl), GetSameLongName(p.Name), p.Price, p.ID);
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string swhere = " 1=1 and IF_DISP=1 ";
        if (Request.QueryString["mx"] != null && Request["mx"].ToString() == "1")
        {
            swhere += " and IF_STAR=1 ";
            titie.Text = "明星产品";
        }
        else if (Request.QueryString["mx"] != null)
        {
            swhere += "  and  T_LEVEL1='" + Request.QueryString["mx"].ToString() + "' ";
            titie.Text = Request.QueryString["mx"].ToString();
            if (titie.Text == "美妆香氛")
                titie.Text = "美妆护肤";
        }
        if (Request.QueryString["top"] != null)
        {
            swhere += " and IF_TOP=1 ";
            titie.Text = "当季推荐";
        }
        if (Request.QueryString["price"] != null)
        {
            if (Request.QueryString["price"].ToString() == "1")
            {
                swhere += " and Price<=800 ";
            }
            else if (Request.QueryString["price"].ToString() == "2")
            {
                swhere += " and price>=801 and price<=1200 ";
            }
            else if (Request.QueryString["price"].ToString() == "3")
            {
                swhere += " and price>1200 ";
            }
            titie.Text = "商城";
        }

        if (Request.QueryString["h"] != null)
            swhere += "  and  T_LEVEL2='" + Request.QueryString["h"].ToString() + "' ";

        swhere += " order by price "+price.Value;

        List<PRODUCT> list = pbo.GetModelList(swhere);
        plist.InnerHtml = "";
        foreach (PRODUCT p in list)
        {
            plist.InnerHtml += string.Format(@"<ol>
   <p>
   <a href='ProductsExchange2.aspx?id={3}'   ><img src='{0}'  style='max-height:287px;' alt='' /></a>
   <span>&nbsp;&nbsp;{1}</span>
   <span>价格：<strong>{2}元</strong></span>
   </p>
   </ol>
            ", p.PicUrl == "" ? "images/m.png" : p.PicUrl, GetSameLongName(p.Name), p.Price, p.ID);
        }
    }

    public string GetSameLongName(string name)
    {
        if (name.Length <= 9)
            return name;

        return name.Substring(0, 9) + "...";
    }
}